﻿using System;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Model.SchedulerModels;
using LiteDB;

namespace DevelopTool.Bll.SchedulerBlls
{
    public class Scheduler_CodeBll
    {
        public Scheduler_CodeBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Scheduler_Code>();
                col.EnsureIndex(x => x.SchedulerId);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }



        /// <summary>
        /// 根据定时任务id获取代码任务信息
        /// </summary>
        /// <param name="codeId"></param>
        /// <returns></returns>
        public Scheduler_Code GetBySchedulerId(int schedulerId)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Scheduler_Code>();

                var scheduler_Code = col.FindOne(it => it.SchedulerId == schedulerId);


                return scheduler_Code;

            }
        }





        /// <summary>
        /// 保存定时任务-代码任务信息
        /// </summary>
        /// <param name=></param>
        /// <returns></returns>
        public Scheduler_Code Save(Scheduler_Code scheduler_Code)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Scheduler_Code>();

                scheduler_Code.UpdateTime = DateTime.Now;
                col.Upsert(scheduler_Code);

                return scheduler_Code;
            }
        }

    }
}
